import { useGlobalStore } from '@/store/global'
import { usePersonStore } from '@/store/person'
import { useHomeStore } from '@/store/home'
import { computed, ref } from 'vue'
import { IdLabel, IdName } from '@/types/global'

export const useGlobalEffect = () => {
  const globalStore = useGlobalStore()
  const personStore = usePersonStore()

  const getAllEnum = () => {
    getSubcontractorList()
    getPersonTypeList()
    getPersonCategoryList()
  }

  // 获取分包单位列表
  const getSubcontractorList = async () => {
    if (globalStore.subcontractorList.length > 0) return
    globalStore.getSubcontractorList()
  }

  // 获取人员类型
  const getPersonTypeList = async () => {
    if (globalStore.personTypeList.length > 0) return
    globalStore.getPersonTypeList()
  }

  // 获取人员分类
  const getPersonCategoryList = async () => {
    if (globalStore.personTypeList.length > 0) return
    globalStore.getPersonCategoryList()
  }

  const subcontractorList = computed(() => globalStore.subcontractorList)
  const reasonList = computed(() => globalStore.personTypeList)
  const personCategoryList = computed(() => globalStore.personCategoryList)

  // 手机号格式验证
  const requiredPhoneValidator = (val:string) => /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(val)
  const phoneValidator = (val:string) => {
    if (!val) return true
    return /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(val)
  }
  // 身份证号格式验证
  let _IDRe18 = /^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
  let _IDre15 =  /^([1-6][1-9]|50)\d{4}\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}$/
  const idCardValidator = (val:string) => _IDRe18.test(val) || _IDre15.test(val)


  const { isolate_status_obj, confirm_status_obj } = personStore
  // 处理个人状态枚举
  const solateStatusName = (id:string) => {
    return isolate_status_obj[id]
  }
  // 处理确认状态枚举
  const confirmStatusName = (id:string) => {
    return confirm_status_obj[id]
  }

  return {
    getAllEnum,
    subcontractorList,
    reasonList,
    personCategoryList,
    requiredPhoneValidator,
    phoneValidator,
    idCardValidator,
    solateStatusName,
    confirmStatusName
  }
}
